package com.wu.dms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wu.dms.cache.UrlCache;
import com.wu.common.convertion.result.Result;
import com.wu.common.convertion.result.Results;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.dms.dao.entity.Song;
import com.wu.dms.dto.SongRequestDTO;
import com.wu.dms.dto.SongRequestPageDTO;
import com.wu.dms.dto.resp.PageResp;
import com.wu.dms.service.SongService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("songs")
@Slf4j
public class SongController {


    private final SongService songService;


    // 添加歌曲
    @PostMapping("/add")
    public Result<Boolean> addSong(SongRequestDTO addSongRequestDTO, @RequestParam("file") MultipartFile file) {
        songService.addSong(addSongRequestDTO, file);
        return Results.success(true);
    }

    // 删除歌曲
    @DeleteMapping("/delete")
    public Result<Boolean> deleteSong(@RequestParam Long id) {
        songService.deleteSong(id);
        return Results.success(true);
    }
    //
    // 返回指定歌曲ID的歌曲
    @GetMapping("/detail")
    public Result<List<Song>> songOfId(@RequestParam Long id) {
        return Results.success(songService.songOfId(id));
    }

    // 返回指定歌手ID的歌曲
    @GetMapping("/singer/detail")
    public Result<PageResp> songOfSingerIdInPage(SongRequestPageDTO requestPageDTO) {
        Page<Song> page = Page.of(requestPageDTO.getPageNum(), requestPageDTO.getPageSize());
        Page<Song> songPage = songService.query().like( "name", requestPageDTO.getSongName())
                .eq("singer_id", requestPageDTO.getSingerId()).page(page);
        List<Song> songList = songPage.getRecords();
        List<Song> collect = songList.parallelStream()
                .peek(song -> {
                    
                    String imageUrl = UrlCache.getUrl(MinIoNameEnum.SONG_IMAGE.getBucketName(), song.getId(),song.getPic());
                    song.setPic(imageUrl);
                    String songUrl = UrlCache.getUrl(MinIoNameEnum.SONG.getBucketName(), song.getId(),song.getUrl());
                    song.setUrl(songUrl);
                }).collect(Collectors.toList());
        PageResp pageResp = new PageResp();
        pageResp.setTotal(songPage.getTotal());
        pageResp.setData(collect);
        return Results.success(pageResp);
    }

    // 返回指定歌手名的歌曲
    @GetMapping("/singerName/detail")
    public Result<List<Song>> songOfSingerName(@RequestParam String name) {
        return Results.success(songService.songOfSingerName(name));
    }

    // 更新歌曲信息
    @PostMapping("/update")
    public Result<Boolean> updateSongMsg(@RequestBody SongRequestDTO updateSongRequestDTO) {
        songService.updateSongMsg(updateSongRequestDTO);
        return Results.success(true);
    }

    // 更新歌曲图片
    @PostMapping("/img/update")
    public Result<Boolean> updateSongPic(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") Long id) {
        songService.updateSongPic(urlFile, id);
        return Results.success(true);
    }

    // 更新歌曲
    @PostMapping("/url/update")
    public Result<Boolean> updateSongUrl(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") Long id) {
        songService.updateSongUrl(urlFile, id);
        return Results.success(true);
    }
}
